#include <akncheckboxsettingpage.h> 
#include <AknQueryDialog.h>

#include <MrLockUi.rsg>
#include <HWRMLight.h>

#include "EnumSettingPage.h"
#include "SettingsBaseView.h"
#include "ProfilesSettingPage.h"
#include "Utils.h"
#include "Logger.h"

CEnumSettingPage* CEnumSettingPage::NewL(const TDesC& aTitle, TInt& aIndex, TMyIntArray& aProfileList, TInt aArrResId, TInt aInd)
{
	CAknQueryValueText* queryValArr = CreateArrayLC(aArrResId, aIndex);

	CEnumSettingPage* page = new (ELeave) CEnumSettingPage(aTitle, aIndex, *queryValArr);

	page->iSettingPageType = EProfileListSettingPage;
	page->iProfileList = &aProfileList;

	CleanupStack::PushL(page);//+page
	page->ConstructL(aTitle, queryValArr, aInd);
	CleanupStack::Pop(4);//-page, queryValArr, valTextArray, arr
	return page;
}

CAknQueryValueText* CEnumSettingPage::CreateArrayLC(TInt aResId, TInt aSelectedItem)
{
	CDesCArray* arr = CEikonEnv::Static()->ReadDesCArrayResourceL(aResId);
	CleanupStack::PushL(arr);//+arr

	CAknQueryValueTextArray* valTextArray = CAknQueryValueTextArray::NewL();
	CleanupStack::PushL(valTextArray);//+valTextArray
	valTextArray->SetArray(*arr);

	CAknQueryValueText* queryValArr = CAknQueryValueText::NewLC();
	//+queryValArr
	queryValArr->SetArrayL(valTextArray);
	queryValArr->SetCurrentValueIndex(aSelectedItem);
	return queryValArr;
}

void CEnumSettingPage::ConstructL(const TDesC& aTitle, CAknQueryValueText* aQueryValArr, TInt aInd)
{
	if (aInd != KErrNotFound)
		SetSettingNumberL(aInd+1);
	SetSettingTextL(aTitle);

	iQueryValArr = aQueryValArr;//this must be last - if leaves occurs
}

CEnumSettingPage::CEnumSettingPage(const TDesC& aTitle, TInt& aIndex, MAknQueryValue& aQueryValue)
:CAknPopupSettingPage(R_ENUM_SETTING_PAGE, aQueryValue)
, iIndex(aIndex), iTitle(aTitle)
{

}

CEnumSettingPage::~CEnumSettingPage()
{
	CAknQueryValueTextArray* valTextArr = (CAknQueryValueTextArray*) iQueryValArr->MdcArray();
	delete valTextArr->Array();
	delete valTextArr;
	delete iQueryValArr;
	iLightInd.Close();
}

void CEnumSettingPage::AcceptSettingL()
{
	LOG_FORMAT( _L("AcceptSettingL(%d)"), iDontSaveSettings );
	if( iDontSaveSettings )
		return;

	iIndex = iQueryValArr->CurrentValueIndex();
}

TBool CEnumSettingPage::TryToShowExtraSettingPage()
{
	LOG_BEGIN("TryToShowExtraSettingPage");

	TBool res = EFalse;

	if( iAlreadyShown )
		return iDontSaveSettings;

	iAlreadyShown = ETrue;

	CAknPopupSettingList* listBox = PopupSettingListBox();
	TInt ind = listBox->CurrentItemIndex();

	if( ind + 1 == iQueryValArr->MdcArray()->MdcaCount() )
	{
		CAknSettingPage* page = NULL;
		switch( iSettingPageType )
		{
			case EProfileListSettingPage:
			{
				page = (CAknSettingPage*) CProfilesSettingPage::NewL(iTitle, *iProfileList, SettingNumber()-1);
				break;
			}
		}

		if( page )
			res = !page->ExecuteLD();
	}
	
	LOG_END("TryToShowExtraSettingPage");
	return res;
}

TBool CEnumSettingPage::OkToExitL(TBool aAccept)
{
	LOG_FORMAT( _L("OkToExitL( %d )"), aAccept );
	
	if( aAccept )
		iDontSaveSettings = TryToShowExtraSettingPage();

	return ETrue;
}

CEnumSettingPage* CEnumSettingPage::NewL(const TDesC& aTitle, TInt& aIndex, RArray<TUid>& aAppList, TInt aArrResId, TInt aInd)
{
	CAknQueryValueText* queryValArr = CreateArrayLC(aArrResId, aIndex);

	CEnumSettingPage* page = new (ELeave) CEnumSettingPage(aTitle, aIndex, *queryValArr);

	page->iSettingPageType = EAppListSettingPage;
	page->iAppList = &aAppList;

	CleanupStack::PushL(page);//+page
	page->ConstructL(aTitle, queryValArr, aInd);
	CleanupStack::Pop(4);//-page, queryValArr, valTextArray, arr
	return page;
}
